//
//  KMCheckTel.m
//  OpenDoor
//
//  Created by M&M on 15/6/15.
//  Copyright (c) 2015年 CNMOBI. All rights reserved.
//

#import "KMCheckTel.h"

@implementation KMCheckTel

+ (void)dialPhoneNumber:(NSString *)aPhoneNumber withView:(UIView *)view
{
    UIWebView*callWebview =[[UIWebView alloc] init];
    aPhoneNumber = [aPhoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *callStr = [NSString stringWithFormat:@"tel://%@",aPhoneNumber];
    
    NSURL *telURL =[NSURL URLWithString:callStr];// 貌似tel:// 或者 tel: 都行
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    //记得添加到view上
    [view addSubview:callWebview];
}

+ (BOOL)checkCodeNum:(NSString *)str andNumlength:(NSInteger )length
{
    
    NSString * codeNum = [NSString stringWithFormat:@"^\\d{%ld}$",(long)length];
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", codeNum];
    
    if (([regextestmobile evaluateWithObject:str] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
+ (BOOL)checkTel:(NSString *)str
{

    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,183,184,187,188，183
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|7[678]|8[0-9])\\d{8}$";

    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    if (([regextestmobile evaluateWithObject:str] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)stringContainsEmoji:(NSString *)string
{
    __block BOOL returnValue = NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                if (0xd800 <= hs && hs <= 0xdbff) {
                                    if (substring.length > 1) {
                                        const unichar ls = [substring characterAtIndex:1];
                                        const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                        if (0x1d000 <= uc && uc <= 0x1f77f) {
                                            returnValue = YES;
                                        }
                                    }
                                } else if (substring.length > 1) {
                                    const unichar ls = [substring characterAtIndex:1];
                                    if (ls == 0x20e3) {
                                        returnValue = YES;
                                    }
                                } else {
                                    if (0x2100 <= hs && hs <= 0x27ff) {
                                        returnValue = YES;
                                    } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                        returnValue = YES;
                                    } else if (0x2934 <= hs && hs <= 0x2935) {
                                        returnValue = YES;
                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                        returnValue = YES;
                                    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                        returnValue = YES;
                                    }
                                }
                            }];
    DLog(@"returnValue = %d",returnValue);
    return returnValue;
}

//检测版本
+(void)CheckVersion
{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    //CFShow((__bridge CFTypeRef)(infoDic));
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
    
    NSString *URL = @"http://itunes.apple.com/lookup?id=1014900674";//id= 你的应用id，具体百度
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
    [request setURL:[NSURL URLWithString:URL]];
    [request setHTTPMethod:@"POST"];
    request.timeoutInterval = 3;
    
    NSHTTPURLResponse *urlResponse = nil;
    NSError *error = nil;
    NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    
    NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
    NSDictionary *dic = [results JSONValue]?[results JSONValue]:nil;
    if (dic == nil) {
        return;
    }
    NSArray *infoArray = [dic objectForKey:@"results"];
    if ([infoArray count]) {
        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
        NSString *lastVersion = [releaseInfo objectForKey:@"version"];
        
        if (![lastVersion isEqualToString:currentVersion]) {

            KMAlertView *alert = [[KMAlertView alloc]initWithTitle:@"检查更新" message:@"有新的版本更新，是否前往更新？" cancelButtonTitle:@"取消" clickButton:^(NSInteger index) {
                if (index == 1) {
                    
                    NSString *appURL = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id1014900674"];
                    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:appURL]];
                }
                
            } otherButtonTitles:@"升级"];
            [alert show];
        }
    }
    
}

@end
